package com.byx.chatserver.controller;

import com.byx.chatserver.entity.FileType;
import com.byx.chatserver.service.IFileService;
import com.byx.springboot.unifiedresponseresult.entity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * FileController
 *
 * @author Superb
 * @date 2021/4/6 9:30
 */
@RestController
@ResponseResult
@RequestMapping("/file")
public class FileController {
    private IFileService fileService;

    @Autowired
    public void setFileService(@Qualifier("fileServiceImpl") IFileService fileService) {
        this.fileService = fileService;
    }

    @PostMapping(value = "/upload",consumes = "multipart/*",headers = "Content-Type=multipart/form-data")
    public String upload(@RequestParam("userId") String userId, @RequestParam("file") MultipartFile file,@RequestParam("type") int type){
        switch (type){
            case 100:
                return fileService.saveFile(file,userId, FileType.IMAGE);
            case 200:
                return fileService.saveFile(file,userId,FileType.AUDIO);
            case 300:
                return fileService.saveFile(file,userId,FileType.VIDEO);
            case 400:
                return fileService.saveFile(file,userId,FileType.AVATAR);
            default:
                return fileService.saveFile(file,userId,FileType.FILE);
        }
    }

//    @ResponseResult
//    @PostMapping(value = "/uploadFiles",consumes = "multipart/*",headers = "Content-Type=multipart/form-data")
//    public String uploadFiles(@RequestParam("userId") String userId, @RequestParam("file") List<MultipartFile> file, @RequestParam("type") int type){
//        switch (type){
//            case 100:
//                return fileService.saveFile(file,userId, FileType.IMAGE);
//            case 200:
//                return fileService.saveFile(file,userId,FileType.AUDIO);
//            case 300:
//                return fileService.saveFile(file,userId,FileType.VIDEO);
//            default:
//                return fileService.saveFile(file,userId,FileType.FILE);
//        }
//    }
}
